/////////////////////////////////////////////////////////
//	File Name : "CMainMenuState.cpp"
//	Author Name : Zach Bolthouse
//	Purpose : Main Menu
/////////////////////////////////////////////////////////

#include "CGame.h"
#include "CMainMenuState.h"
#include "CGamePlayState.h"
//#include "COptionsState.h"
//#include "CCreditsState.h"
//#include "CProfileSelectState.h"
//#include "CSaveLoadState.h"

#define TITLEXPOSITION 100
#define TITLEYPOSITION 50
#define CURSORXPOSITION	220
#define MENUXPOSITION 250
#define MENUYPOSITION 150
#define MENUOFFSET 35
enum menu{PLAY, OPTIONS, CREDITS, EXIT, TOTALMENUITEMS};

CGamePlayState::CGamePlayState()
{
	m_pD3D			=	NULL;
	m_pTM			=	NULL;
	m_pFM			=	NULL;
	m_pDI			=	NULL;
	m_pBF			=	NULL;
	//m_pPM			=	NULL;
	m_nImageID		=	-1;
	m_nCursorImageID = -1;
	m_nCursorSelectID = -1;
	m_nCursorMoveID = -1;
	m_bEnter		=	false;
	m_bInputBuffer	=	false;
	m_nSelection	=	0;
	m_fUpDownTimer  =	0.0f;
}

CGamePlayState::CGamePlayState(const CGamePlayState&)
{

}

CGamePlayState& CGamePlayState::operator = (const CGamePlayState&)
{
	return *this;
}

CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}

void CGamePlayState::Enter()
{
	// Get pointers to singletons
	m_pD3D	=	CSGD_Direct3D::GetInstance();
	m_pTM	=	CSGD_TextureManager::GetInstance();
	m_pFM	=	CSGD_FModManager::GetInstance();
	m_pDI	=	CSGD_DirectInput::GetInstance();
	m_pBF   =   CBitmapFont::GetInstance();
	m_pEM = CEventSystem::GetInstance();
	//m_pPM	=	CProfileManager::GetInstance();

	//int active = m_pPM->GetActiveProfile();
	//float bg = (float)m_pPM->GetProfiles()[active]->GetBGM();
	//float sfx = (float)m_pPM->GetProfiles()[active]->GetSFX();
	//float pannn = (float)m_pPM->GetProfiles()[active]->GetPan();
	//CGame::GetInstance()->SetBGMVolume(bg);
	//CGame::GetInstance()->SetSFXVolume(sfx);
	//CGame::GetInstance()->SetPanning(pannn);

	//Load Textures
	m_nCursorImageID = m_pTM->LoadTexture("resource/Menu/MainMenuCursor.png", D3DCOLOR_XRGB(0, 0, 0));
	m_nImageID = m_pTM->LoadTexture("resource/Menu/MainMenuBackground.png", D3DCOLOR_XRGB(0, 0, 0));

	//Load Sounds
	m_nCursorSelectID = m_pFM->LoadSound("resource/sounds/menusounds/select.wav");
	m_nCursorMoveID = m_pFM->LoadSound("resource/sounds/menusounds/move.wav");
	CAnimationManager::GetInstance()->FindAnim("YoumuStand",&anim);
	m_nBGStartID = m_pFM->LoadSound("resource\\\\music\\LevelMusic\\nightofKnights.mp3", SGD_FMOD_LOOPING);
	//CSGD_FModManager::GetInstance()->SetVolume(m_nBGSoundID, CGame::GetInstance()->GetBGMVolume());
	//CSGD_FModManager::GetInstance()->SetPan(m_nBGStartID, CGame::GetInstance()->GetPanning());
	m_pFM->PlaySound(m_nBGStartID);
	m_bEnter = false;
	m_bQuit = false;
	m_bInputBuffer = false;
	m_nSelection = 0;
	m_fUpDownTimer = 0.0f;
	m_RenderRect = CGame::GetInstance()->GetScreenRect();
	m_Player = new CCharacter;
	m_Player->SetCharacter("Youmu");
	m_Player->SetPosX(500);
	m_Player->SetPosY(500);

}

bool CGamePlayState::Input()
{
	if(m_bQuit)
		return false;
	m_bEnter = false;
	if(CGame::GetInstance()->GetGamePad())
	{
		XINPUT_STATE xState;
		DWORD erro = XInputGetState(0, &xState);
		if(xState.Gamepad.wButtons & XINPUT_GAMEPAD_A || xState.Gamepad.wButtons & XINPUT_GAMEPAD_B || xState.Gamepad.wButtons & XINPUT_GAMEPAD_Y || xState.Gamepad.wButtons & XINPUT_GAMEPAD_X || xState.Gamepad.wButtons & XINPUT_GAMEPAD_START)
		{
			if(m_bInputBuffer)
			{
				m_bInputBuffer = false;
				//CSGD_FModManager::GetInstance()->SetVolume(m_nCursorSelectID, CGame::GetInstance()->GetSFXVolume());
				//CSGD_FModManager::GetInstance()->SetPan(m_nCursorSelectID, CGame::GetInstance()->GetPanning());
				m_pFM->PlaySound(m_nCursorSelectID);
				m_bEnter = true;
			}
		}	
		else 
			m_bInputBuffer = true;
		float fY = xState.Gamepad.sThumbLY/24383.5f;
		if(fY > .5f && m_fUpDownTimer > .25f)
		{
			//CSGD_FModManager::GetInstance()->SetVolume(m_nCursorSelectID, CGame::GetInstance()->GetSFXVolume());
			//CSGD_FModManager::GetInstance()->SetPan(m_nCursorSelectID, CGame::GetInstance()->GetPanning());
			m_pFM->PlaySound(m_nCursorMoveID);
			m_fUpDownTimer = 0.0f;
			m_nSelection--;
			if(m_nSelection < PLAY)
				m_nSelection = EXIT;
		}
		else if(fY < -.5f && m_fUpDownTimer > .25f)
		{
			//CSGD_FModManager::GetInstance()->SetVolume(m_nCursorSelectID, CGame::GetInstance()->GetSFXVolume());
			//CSGD_FModManager::GetInstance()->SetPan(m_nCursorSelectID, CGame::GetInstance()->GetPanning());
			m_pFM->PlaySound(m_nCursorMoveID);
			m_fUpDownTimer = 0.0f;
			m_nSelection++;
			if(m_nSelection > EXIT)
				m_nSelection = PLAY;
		}
	}
	else
	{
		if(m_pDI->KeyDown(DIK_DOWN))
		{
			m_pEM->SendEvent("MoveDown");
		}
		if(m_pDI->KeyDown(DIK_UP))
		{
			m_pEM->SendEvent("MoveUp");
		}
		if(m_pDI->KeyDown(DIK_RIGHT))
		{
			m_pEM->SendEvent("MoveRight");
		}
		if(m_pDI->KeyDown(DIK_LEFT))
		{
			m_pEM->SendEvent("MoveLeft");
		}
		if(m_pDI->KeyDown(DIK_S))
		{
			m_pEM->SendEvent("ButtonD");
		}
	}


	return true;
}

void CGamePlayState::Update(float fElapsedTime)
{
	m_fUpDownTimer += fElapsedTime;
	m_pEM->ProcessEvents();
	m_Player->Update(fElapsedTime);
	if(m_bEnter)
	{
		switch(m_nSelection)
		{
		case PLAY:			
			//CGame::GetInstance()->ChangeState(CSaveLoadState::GetInstance());
			break;
		case OPTIONS:
			//CGame::GetInstance()->AddState(COptionsState::GetInstance());
			break;
		case CREDITS:
			//CGame::GetInstance()->ChangeState(CCreditsState::GetInstance());
			break;
		case EXIT:
			m_bQuit = true;
			break;
		}
	}
}

void CGamePlayState::Render(int bright)
{
	//Render stuff
	m_pTM->Draw(m_nImageID, 0, 0, 1.0f, 1.0f, &m_RenderRect, 0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(bright, bright, bright));
	m_Player->Render();
	//m_pD3D->GetSprite()->Flush();
	//m_pBF->Print((char*)(m_pPM->GetProfiles()[m_pPM->GetActiveProfile()]->GetName().c_str()), 50, 550,.5f, 0xFFFFFFFF);

}

void CGamePlayState::Exit()
{
	//m_pTM->UnloadTexture(m_nCursorImageID);
	m_pTM->UnloadTexture(m_nImageID);
	//m_pFM->UnloadSound(m_nCursorMoveID);
	//m_pFM->UnloadSound(m_nCursorSelectID);
	//m_pFM->UnloadSound(m_nBGSoundID);
}
